package com.example.controller;


import com.example.domain.Admin;
import com.example.domain.Result;
import com.example.service.AdminService;
import com.example.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;


@Controller
public class LoginController {
    @Autowired
    public AdminService adminService;
    @Autowired
    public UserService userService;

    @PostMapping("tologin")
    public String login(String username, String password, HttpSession session) {
        Admin admin = adminService.findByUsernameAndPassword(username, password);
//        System.out.println(admin);
        if (admin != null) {
            session.setAttribute("admin", admin);
            session.removeAttribute("msg");

            return "redirect:main";
        }
//        model.addAttribute("msg","用户名或密码不正确");
        session.setAttribute("msg", "用户名或密码不正确");
        return "redirect:login";
    }

    @PostMapping("tovxlogin")
    @ResponseBody
    public Result vxlogin(String username, String password, HttpSession session) {
        return userService.findByUsernameAndPassword(username, password, session);
    }


}
